/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Loaiphim;
import utils.HibernateUtil;
import pojo.Datcho;

/**
 *
 * @author Dang Nhung
 */
public class DatChoDAO {

    public static SessionFactory _localSessionFactory;

    public static final String CODE_PLACE_TEMPLATE = "B%d";

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }

    public static String generateCodePlace() {
        String rs = String.format(CODE_PLACE_TEMPLATE, System.nanoTime());
        return rs;
    }

    public static List<Datcho> getDatCho() throws Exception {

        List<Datcho> items = null;
        Session localSession = null;
        String localQueryString = "FROM Datcho dc Where dc.trangThai= :statusDatCho ";
        try {
            // Step 01: create retrieving session using where clause
            localSession = _localSessionFactory.openSession();
            // Step 02 : begin retrieving transaction in above session.
            localSession.beginTransaction();
            // Step 03 : set parameters and retrieving objects.
            Query q = localSession.createQuery(localQueryString);
            q.setInteger("statusDatcho", 1);
            items = q.list();
            // Step 04 : commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception ex) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getDatCho", DatChoDAO.class.getName(), ex.getMessage()));
        } finally {
            try {
                // Step 05: close retrieving session
                localSession.close();
            } catch (Exception ex) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getDatCho", DatChoDAO.class.getName(), ex.getMessage()));
            }
        }
        return items;
    }

    public static List<Datcho> getDatChoByCodeDatCho(String codeDatCho) throws Exception {
        List<Datcho> items = null;
        Session localSession = null;
        String localQuery = "FROM Datcho dc WHERE dc.codeDatCho = :codeDatCho AND dc.trangThai = :statusDatCho";
        try {
            //Step 01: validate input name
            if (codeDatCho.isEmpty()) {
                throw new Exception("Param not valid");
            }
            //Step 02: create retrieving session using where clause
            localSession = _localSessionFactory.openSession();
            //Step 03: begin retrieving transaction in above session
            localSession.beginTransaction();
            //Step 04: set parameters and retrieving objects
            Query q = localSession.createQuery(localQuery);
            q.setString("codeDatCho", codeDatCho);
            q.setInteger("statusDatCho", 1);
            items = q.list();
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception ex) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getDatChoByCodeDatCho", DatChoDAO.class.getName(), ex.getMessage()));

        } finally {
            try {
                //Step 06: close retrieving data 
                localSession.close();
            } catch (Exception ex) {
                throw new Exception(String.format("Failed in %s of %s", "getDatChoByCodeDatCho", DatChoDAO.class.getName(), ex.getMessage()));
            }
        }
        return items;
    }

    public static boolean insertDatCho(Datcho paramDatCho) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input name
            if (paramDatCho == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create insert session
            localSession = _localSessionFactory.openSession();
            //Step 03:
            localSession.beginTransaction();
            //Step 04: save object into database
            localSession.save(paramDatCho);
            //Step 05: commit and insert transaction in above session
            localSession.getTransaction().commit();
        } catch (Exception ex) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "inserDatCho", DatChoDAO.class.getName(), ex.getMessage()));
        } finally {
            try {
                //Step 06: close insert session
                localSession.close();
            } catch (Exception ex) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "inserDatCho", DatChoDAO.class.getName(), ex.getMessage()));
            }
        }
        return rs;
    }

    public static boolean updateDatCho(Datcho paramDatCho) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input name
            if (paramDatCho == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create update session
            localSession = _localSessionFactory.openSession();
            //Step 03: begin update transaction in above session
            localSession.beginTransaction();
            //Step 04: update object into database
            localSession.update(paramDatCho);
            //Step 05: commit and update transaction in above session
            localSession.getTransaction().commit();
        } catch (Exception ex) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s", "updateDatCho", DatChoDAO.class.getName(), ex.getMessage()));
        } finally {
            try {
                //Step 06: close update session
                localSession.close();
            } catch (Exception ex) {
                throw new Exception(String.format("Failed in %s of %s", "updateDatCho", DatChoDAO.class.getName(), ex.getMessage()));
            }
        }
        return rs;
    }

    public static boolean deleteDatCho(Datcho paraDatcho) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paraDatcho == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: update object into database.
            localSession.delete(paraDatcho);
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s", "deleteDatCho", DatChoDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close update session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "deleteDatCho", DatChoDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }
}
